/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author duanzuocai
 * @since 2018/11/17
 */
layui.define(['form', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var layedit = layui.layedit;
    var laydate = layui.laydate;

    // 提交
    form.on('submit(zmn-permitdata-form-submit)', function (obj) {
        let openId = layer.open({type: 3});
        let data = obj.field;
        let url = "/mc/permit/data/add.action";

        let checkedPermit = '';

        let permitList = new Array();
        $("input:checkbox[name=permit_check]:checked").each(function (j) {
            permitList[j] = $(this).val();
        });

        checkedPermit = permitList.length > 0 ? permitList.join(",") : 0;

        data.typeDataIds = checkedPermit;

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success: function (json) {
                parent.reload(json.message);
                cancel();
            },
            error: function () {
                cancel();
            }
        })
        return false;
    });

    let companys = json.companys;
    layui.$("#data_type_div_id").html(loadPermitdata(companys));


    form.on('checkbox(permit_all)', function (data) {
        if (data.elem.checked) {
            $("input:checkbox[name=permit_all]").prop("checked", true);
            $("input:checkbox[name=permit_check]").prop("checked", false);
            form.render("checkbox");
        } else {
            let cityList = new Array();
            $("input:checkbox[name=permit_check]:checked").each(function (j) {
                cityList[j] = $(this).val();
            });
            if (cityList.length == 0) {
                $("input:checkbox[name=permit_all]").prop("checked", true);
                form.render("checkbox");
            }
        }

    });

    form.on('checkbox(permit_check)', function (data) {
        let cityList = new Array();
        $("input:checkbox[name=permit_check]:checked").each(function (j) {
            cityList[j] = $(this).val();
        });
        if (cityList.length > 0) {
            $("input:checkbox[name=permit_all]").prop("checked", false);
            form.render("checkbox");
        } else {
            $("input:checkbox[name=permit_all]").prop("checked", true);
            form.render("checkbox");
        }
    });

    //渲染二级分类数据
    form.render(null, "zmn-permitdata-form");

    zmnMCPage.laySelectLinkage(form, "/common/mc/permitdata/listChilds.action", "typeFilter");

    //对外暴露的接口
    exports('art_permitdata_detail', {});

});

let fristInFlag = true;

let zmnMCPage = {
    /**
     * 多级下拉框联动
     * @param form layui.form
     * @param url 请求下级数据的url，返回数据格式 [[value,text], [value,text], [value,text], …]
     * @param selectOneId 第1级select的ID，不含#号
     * @param selectTwoId 第2级select的ID，不含#号
     */
    laySelectLinkage: function (form, url, typeId) {
        let $ = layui.$;
        let type = $("#type").val();

        form.on("select(" + typeId + ")", function (obj) {
            if (zmnUtil.isNull(obj) || obj.value == 0) {
                return;
            }
            type = obj.value;
            $.ajax({
                type: "GET", url: url,
                dataType: "json", data: {parentId: type},
                success: function (items) {
                    layui.$("#data_type_div_id").html(loadPermitdata(items, type));
                    form.render("checkbox");
                }
            });
        });
    },
}

function loadPermitdata(datas, type) {
    type = zmnUtil.isEmptyNull(type) ? 1 : type;
    let checkboxHtml = "";
    let permitDataList = json.permitDataList;

    let flag = true;
    if (permitDataList.length == 0) {
        flag = false;
    } else {
        flag = isChecked(0, type);
    }

    if (flag) {
        checkboxHtml = '<input type="checkbox" checked lay-filter="permit_all" name="permit_all" value="0" title="全部" >';
    } else {
        checkboxHtml = '<input type="checkbox" lay-filter="permit_all" name="permit_all" value="0" title="全部" >';
    }

    for (let i = 0; i < datas.length; i++) {
        if (permitDataList.length == 0) {
            flag = false;
        } else {
            if (fristInFlag) {
                flag = isChecked(datas[i].companyId, type);
            } else {
                flag = isChecked(datas[i][0], type);
            }
        }

        if (fristInFlag) {
            if (flag) {
                checkboxHtml += '<input type="checkbox" lay-filter="permit_check" checked name="permit_check" value="' + datas[i].companyId + '" title="' + datas[i].name + '" >';
            } else {
                checkboxHtml += '<input type="checkbox" lay-filter="permit_check" name="permit_check" value="' + datas[i].companyId + '" title="' + datas[i].name + '" >';
            }
        } else {
            if (flag) {
                checkboxHtml += '<input type="checkbox" lay-filter="permit_check" checked name="permit_check" value="' + datas[i][0] + '" title="' + datas[i][1] + '" >';
            } else {
                checkboxHtml += '<input type="checkbox" lay-filter="permit_check" name="permit_check" value="' + datas[i][0] + '" title="' + datas[i][1] + '" >';
            }
        }


    }
    fristInFlag = false;
    return checkboxHtml;
}

function isChecked(companyId, type) {
    let permitDataList = json.permitDataList;
    for (let i = 0; i < permitDataList.length; i++) {
        var jsonPermit = permitDataList[i];
        if (jsonPermit.typeData == companyId && jsonPermit.type == type) {
            return true;
        }
    }
    return false;
}

function cancel() {
    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
}